package learn.arc.mybatis;

import org.apache.ibatis.builder.SqlSourceBuilder;
import org.apache.ibatis.builder.xml.XMLConfigBuilder;
import org.apache.ibatis.builder.xml.XMLMapperBuilder;
import org.apache.ibatis.executor.BaseExecutor;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.executor.parameter.ParameterHandler;
import org.apache.ibatis.executor.resultset.DefaultResultSetHandler;
import org.apache.ibatis.executor.resultset.ResultSetHandler;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlSource;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.reflection.Reflector;
import org.apache.ibatis.scripting.defaults.DefaultParameterHandler;
import org.apache.ibatis.session.*;
import org.apache.ibatis.session.defaults.DefaultSqlSession;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration;
import org.mybatis.spring.mapper.MapperFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.stereotype.Repository;

/**
 * @Description:
 * @Version 1.0, 2021/9/17
 * @Author Tim
 */
public class Mybatis {
    public void mybatis() {
        SqlSessionFactoryBean f;
        XMLConfigBuilder c2;//解析config
        XMLMapperBuilder c3;//解析mapper
        MappedStatement c4;//
        SqlSource c5;//
        SqlSourceBuilder c6;//
        //xml-->datasource、sqlsession、mapper
        SqlSessionFactory ssf=null;
        ssf.openSession().selectList(null);
        SqlSessionFactoryBuilder ssfb;// DefaultSqlSessionFactory
        XMLConfigBuilder xcb;// 解析配置文件，构建configuration、statement等
        XMLMapperBuilder xmb;// 解析mapper
        Configuration conf;// env+
        Environment env;// datasource+transaction

        StatementHandler sh;
        ParameterHandler ph;// 设置参数
    }

    /**
     *
     */
    public static void mainConcept(){
        Configuration configuration;
        {
            Environment environment;// 数据源、事务
            // setting.xml、mapper.xml 的map
        }
        SqlSessionFactory sqlSessionFactory;
        SqlSession sqlSession;
        // xml反射代理实现？

        // mybatis拦截器
        Interceptor interceptor;

        Executor executor;
            BaseExecutor e;
        MappedStatement mappedStatement;
        StatementHandler statementHandler;
        ParameterHandler parameterHandler;
        ResultHandler handler;

        ResultSetHandler resultSetHandler;
        DefaultResultSetHandler defaultResultSetHandler;

        Reflector reflector;



    }

    /**
     * 整合spring http://mybatis.org/spring/zh/index.html
     * 注解处理
     * bean管理
     *
     */
    public static void spring(){
        /* step1，注解处理*/
        // 标记@Component
        Repository repository;
        MapperScan mapperScan;


        /* step2，bean管理*/
        // 实现FactoryBean，获取SqlSessionFactory
        // InitializingBean，创建SqlSessionFactory
        // 实现ApplicationListener，bf刷新后check all statements are completed
        // xml--mapper
        SqlSessionFactoryBean sqlSessionFactoryBean;
        SqlSession sqlSession;
        {
            DefaultSqlSession defaultSqlSession;// 线程不安全
            SqlSessionTemplate sqlSessionTemplate;// 线程安全，每次重新获取
            SqlSessionTemplate.SqlSessionInterceptor sqlSessionInterceptor;
            SqlSessionDaoSupport sqlSessionDaoSupport;// 继承，获取sqlSessionTemplate
        }
        // BeanDefinitionRegistryPostProcessor；扫描指定路径，过滤后注册bd。
        // 按注解、父标记接口；默认不过滤
        MapperScannerConfigurer mapperScannerConfigurer;
        // mapper注册为MapperFactoryBean；SqlSession.getMapper获取mapper
        // autowire时调用setSqlSessionFactory
        MapperFactoryBean mapperFactoryBean;

    }

    /**
     * springboot整合 https://www.cnblogs.com/java-chen-hao/p/11849869.html
     *
     */
    public static void springboot(){
        // 启动配置类，spi加载
        // 导入必须的bean
        MybatisAutoConfiguration autoConfiguration;


    }

    /**
     * 批量操作
     * @see tk.mybatis.mapper.additional.insert.InsertListMapper 不支持主键策略
     * @see tk.mybatis.mapper.common.special.InsertListMapper 必须为自增主键
     */
    public static void tkMybatisPlus(){

    }

    /**
     * 批量操作
     * 继承 ServiceImpl；但批量插入，还是逐条插入的
     */
    public static void baomidouMybatisPlus(){

    }

    /**
     *
     */
    public static void printDebugSql(){
        // System.out.println(((PreparedStatementProxyImpl) ((DruidPooledPreparedStatement) ps).stmt).raw); return false;
        DefaultParameterHandler h1=null;
        h1.setParameters(null);// 断点在方法尾


    }

}
